package com.example.controller;


import cn.hutool.core.date.DateUtil;
import com.example.base.response.Result;
import com.example.entity.Nurse;
import com.example.entity.Params;
import com.example.entity.Reserve;
import com.example.mapper.NurseMapper;
import com.example.service.ReserveService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Api(tags = "服务记录管理")
@CrossOrigin
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Autowired
    private ReserveService reserveService;
    @Autowired
    private NurseMapper nurseMapper;


    @PostMapping("/save")
    public Result save(@RequestBody Reserve reserve) {
        Nurse nurse = nurseMapper.selectById(reserve.getServiceid());
        if (nurse.getNum() == 0) {
            return Result.error("该服务已无剩余员工");
        }
        reserve.setTime(DateUtil.now());
        reserveService.add(reserve);
        nurse.setNum(nurse.getNum() - 1);
        nurseMapper.update(nurse);
        return Result.success();
    }


    @GetMapping("/search")
    public Result findBySearch(Params params) {
        PageInfo<Reserve> info = reserveService.findBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        reserveService.delete(id);
        return Result.success();
    }


}
